//
//  HBMeInfoCell.m
//  HeartBeat
//
//  Created by karong on 2023/9/4.
//

#import "HBMeInfoCell.h"

@interface HBMeInfoCell ()
@property (weak, nonatomic) IBOutlet UIImageView *userImageView;
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UIView *vipView;
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *userSignLabel;
@property (weak, nonatomic) IBOutlet UIButton *goBtn;
@property (weak, nonatomic) IBOutlet UIButton *vipBtn;
@property (weak, nonatomic) IBOutlet UIButton *coinBtn;
@property (weak, nonatomic) IBOutlet UILabel *vip_expires_textLabel;
@property (weak, nonatomic) IBOutlet UILabel *coinsLabel;
@end

@implementation HBMeInfoCell
- (void)setDataWithObject:(HBUserCenterModel *)object {
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:object.avatar] placeholderImage:appUserCenter.isLogin?IMG(@"icon_tx_p"):HBPlaceholderImage];
    self.userNameLabel.text = object.user_name;
    self.userSignLabel.text = (object.id > 0)?SF(@"ID:%ld", (long)object.id):@"";
    self.vip_expires_textLabel.attributedText = [self attrNum:appUserCenter.vip_expires_days type:0 imageStr:@"icon_vipb"];
    self.coinsLabel.attributedText = [self attrNum:appUserCenter.coins type:1 imageStr:@"icon_jinb"];
    self.vipView.hidden = appUserCenter.vip_status == 1;
}

- (NSMutableAttributedString *)attrNum:(NSInteger)num type:(NSInteger)type imageStr:(NSString *)imageStr {
    
    NSString * contentStr = @"";
    if (type == 0) {
        contentStr = SF(WSLocalized(@"剩餘天數%ld天"), (long)num);
    }
    else {
        contentStr = SF(WSLocalized(@"剩餘書幣%ld"), (long)num);
    }
    NSMutableAttributedString *contentAttried = [[NSMutableAttributedString alloc] initWithString:contentStr
                                                                                       attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14 weight:(UIFontWeightMedium)], NSForegroundColorAttributeName: UIColor.font_black_1A1A1A}];
    NSString *string = SF(@"%ld", (long)num);
    NSRange range = [contentStr rangeOfString:string];
    [contentAttried addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:20 weight:(UIFontWeightMedium)], NSForegroundColorAttributeName: [UIColor colorFromHexRGB:@"FF4700"]} range:range];
    if (!KIsNullOrEmpty(imageStr)) {
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        attch.image = [UIImage imageNamed:imageStr];
        attch.bounds = CGRectMake(0, -4, 20, 20);
        // 创建带有图片的富文本
        NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:attch];
        [contentAttried appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
        [contentAttried appendAttributedString:imageStr];
    }
    return contentAttried;
}

- (NSMutableAttributedString *)attrString:(NSMutableAttributedString *)contentAttried addImage:(NSString *)imageStr {
    if (!KIsNullOrEmpty(imageStr)) {
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        attch.image = [UIImage imageNamed:imageStr];
        attch.bounds = CGRectMake(0, -2, 20, 20);
        // 创建带有图片的富文本
        NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:attch];
        [contentAttried appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
        [contentAttried appendAttributedString:imageStr];
    }
    return contentAttried;
}

- (IBAction)userInfoAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(mineUserCellUserAction)]) {
        [self.delegate mineUserCellUserAction];
    }
}

- (IBAction)goAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(mineUserCellGoAction)]) {
        [self.delegate mineUserCellGoAction];
    }
}
- (IBAction)vipAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(mineUserCellVipAction)]) {
        [self.delegate mineUserCellVipAction];
    }
}
- (IBAction)coinAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(mineUserCellCoinAction)]) {
        [self.delegate mineUserCellCoinAction];
    }
}
@end
